package com.dream.mqpushserver.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.ByteBuffer;

/**
 * @author song_
 * @Date 2020/3/26
 * @desc
 **/
public class EnCoder extends MessageToByteEncoder<RequestCommand> {

    @Override
    protected void encode(ChannelHandlerContext ctx, RequestCommand command, ByteBuf byteBuf) throws Exception {

        try {
            ByteBuffer byteBuffer = command.buildBytes();
            System.out.println("send data length " + byteBuffer.limit() + "\t ...");
            byteBuf.writeBytes(byteBuffer);
        } catch (Exception ex) {
            ctx.channel().close();
        }
    }
}
